
const app=getApp();
var douban=app.globalData.doubanUrl;
import utils from '../../utils/utils';
const http=utils.http;
const star=utils.star;
Page({
  data: {
       "in_theaters":{movies:[],title:null},
       "coming_soon":{},
       "top250":{}
  },
  onLoad: function (options) {
    var count="?start=0&count=3";
    var inTheatersUrl=douban+"in_theaters"+count;
    var comingSoon=douban+"coming_soon"+count;
    var top250=douban+"top250"+count;
    http(inTheatersUrl,this.handleData,"in_theaters");
    http(comingSoon,this.handleData,"coming_soon");
    http(top250,this.handleData,"top250");
  },
  handleData(res,type){
    var title=res.data.title;
    var subjects=res.data.subjects;
    var movies=[];
    subjects.forEach(ele=>{
      var average=ele.rating.average;
      var stars=star(ele.rating.stars);
      var title=ele.title;
      var imgUrl=ele.images.small;
      var id=ele.id
      var temp={
        average,
        stars,
        title,
        imgUrl,
        id
      };
      movies.push(temp);
    })
    var readyData={};
    readyData[type]={
      movies,
      title,
      type
    };
    this.setData(readyData);
    wx.hideLoading();
  },
  more(event){
    var type=event.currentTarget.dataset.type;
    var title=event.currentTarget.dataset.title;
    wx.navigateTo({
      url: '../movies/movies-more/movies-more?type='+type+"&title="+title
    })
  },
  // onClick(event){
  //   var id=event.currentTarget.dataset.id;
  //   wx.navigateTo({
  //     url: '/pages/web-page/web-page?id='+id
  //   })
  // },
  onClick(event){
    var id=event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/movies/movies-detail/movies-detail?id='+id,
    })
  },
  onSearch(){
    wx.navigateTo({
      url: '../search/search'
    })
  },
})
      // var inTheatersUrl=douban+"in_theaters"+"?start=0&count=3";
        // var self=this;
        // wx.request({
        //   url: inTheatersUrl,
        //   header: {
        //     'Content-Type': 'json'
        //   },
        //   success: function(res) {
        //     var subjects=res.data.subjects;
        //     var title=res.data.title;
        //     var movies=[];
        //     subjects.forEach(ele=>{
        //       var temp={
        //            title:ele.title,
        //            stars:ele.rating.stars,
        //            average:ele.rating.average,
        //            imgUrl:ele.images.small
        //       };
        //       movies.push(temp);
        //     });
        //     self.setData({
        //       movies:movies
        //     });
        //   }
        // })
// })